<template>
  <div>
    <search btnText="安排电影" @searchMovieSchedule="searchMovieSchedule"></search>
    <table-info :headList="headList" :tableList="tableList" type="schedule" @deleteMovieSchedule="deleteMovieSchedule"></table-info>
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="8"
      @current-change="change"
    />
    <dia-log></dia-log>
  </div>
</template>

<script>
import DiaLog from './components/dialog.vue'
import { getCurrentPageMovieSchedule, deleteMovieSchedule } from '@/api/row'
import { mapState } from 'vuex'
export default {
  components: { DiaLog },
  created () {
    this.getCurrentPageMovieSchedule(
      this.currentPage,
      this.pageSize,
      this.input
    )
  },
  data () {
    return {
      headList: [
        { id: 1, label: '排片ID', prop: 'schedule_id' },
        { id: 2, label: '电影名', prop: 'name' },
        { id: 4, label: '影院名', prop: 'cinema_name' },
        { id: 5, label: '放映大厅', prop: 'hall_name' },
        { id: 6, label: '放映日期', prop: 'show_date' },
        { id: 7, label: '售价(元)', prop: 'price' }
      ],
      tableList: [],
      currentPage: 1,
      pageSize: 8,
      input: '',
      total: 0
    }
  },
  computed: {
    ...mapState('schedule', ['isDialog'])
  },
  methods: {
    // 初始化
    async getCurrentPageMovieSchedule (currentPage, pageSize, input) {
      const { total, data } = await getCurrentPageMovieSchedule({
        currentPage,
        pageSize,
        input
      })
      this.total = total
      this.tableList = data
    },
    // 切换页码
    change (page) {
      this.currentPage = page
      this.getCurrentPageMovieSchedule(this.currentPage, this.pageSize, this.input)
    },
    searchMovieSchedule (value) {
      this.input = value
      this.getCurrentPageMovieSchedule(this.currentPage, this.pageSize, this.input)
    },
    // 删除
    async deleteMovieSchedule (value) {
      if ((this.total - 1) % this.pageSize === 0 && this.currentPage > 1) {
        this.currentPage -= 1
      }
      const res = await deleteMovieSchedule({ scheduleId: value })
      if (res.success_code === 200) {
        this.$message.success('删除成功')
        this.getCurrentPageMovieSchedule(this.currentPage, this.pageSize, this.input)
      } else {
        this.$message.error(res.message)
      }
    }
  }
}
</script>

<style>
</style>
